<template>
  <div>
    <div style="display: flex">
      <el-input
        type="textarea"
        placeholder="请输入留言"
        v-model="inputValue"
        show-word-limit
      >
      </el-input>
      <el-button
        type="primary"
        @click="addMessage"
        :loading="isPosting"
        :disabled="isPosting"
        >提交留言</el-button
      >
    </div>
    <div class="my-message-container" v-loading="isLoading">
      <div
        v-for="(item, index) in $store.state.message.allMessage"
        :key="index"
      >
        <span class="time">{{ item.timestamp }}</span>
        <p class="message-item">
          {{ item.content }}
          <i class="el-icon-delete" @click="deleteMessage(index)"></i>
        </p>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      inputValue: "",
      allMessage: [],
      isPosting: false,
      isLoading: false,
    };
  },
  methods: {
    addMessage() {
      this.isPosting = true;
      setTimeout(() => {
        this.$store.commit("addMessage", this.inputValue);
        this.$message.success("添加成功");
        this.isPosting = false;
        this.inputValue = "";
      }, 500);
    },
    deleteMessage(index) {
      this.$confirm("确认删除").then(() => {
        this.isLoading = true;
        setTimeout(() => {
          this.$store.commit("deleteMessage", index);
          this.$notify({
            type: "success",
            message: "删除成功",
          });
          this.isLoading = false;
        }, 500);
      });
    },
  },
};
</script>
<style lang="less" scoped>
.block {
  margin-top: 30px;
}
.my-message-container {
  .time {
    text-align: center;
    font-size: 12px;
    margin: 10px 0;
  }
  .message-item {
    width: 70%;
    background-color: #fff;
    border-radius: 4px;
    border: #e2e2e2 solid 1px;
    font-weight: bold;
    min-height: 30px;
    padding: 15px;
    text-align: left;
    position: relative;
    .el-icon-delete {
      width: 30px;
      height: 30px;
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      top: -5px;
      right: -5px;
      font-size: 20px;
      background-color: red;
      border-radius: 50%;
      color: #fff;
      cursor: pointer;
    }
  }
}
</style>
